<script lang="ts" setup>
import { ref } from 'vue'
import { isEmpty } from '../../utils/common.ts'
import { addTeamsService, updateTeamsService } from '../../api/teams.ts'

const formRules = {
  teamName: [
    { required: true, message: '请输入战队名', trigger: 'blur' },
    {
      pattern: /^[\S]{1,10}$/, // 非空字符，长度 1-10
      message: '战队名必须是1-10位的非空字符',
      trigger: 'blur',
    },
  ],
  logo: [
    { required: true, message: '请上传队伍队标', trigger: 'blur' },
    {
      pattern: /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/, // URL 校验规则
      message: '队标必须是一个有效的URL',
      trigger: 'blur',
    },
  ],
  members: [
    { required: true, message: '请输入队伍成员', trigger: 'blur' },
    {
      pattern: /^[\S]{1,15}$/, // 非空字符，长度 1-15
      message: '队伍成员必须是1-15位的非空字符',
      trigger: 'blur',
    },
  ],
  slogan: [
    { required: true, message: '请输入队伍口号', trigger: 'blur' },
    {
      pattern: /^[\S]{1,30}$/, // 非空字符，长度 1-30
      message: '队伍口号必须是1-30位的非空字符',
      trigger: 'blur',
    },
  ],
  creationDate: [
    { required: true, message: '请输入队伍成立时间', trigger: 'blur' },
    {
      pattern: /^[1-9][0-9]{0,2}$/, // 排名必须是1到999之间的数字
      message: '队伍排名必须是1-999的数字',
      trigger: 'blur',
    },
  ],
}

const formModel = ref({
  teamName: '',
  logo: '',
  members: '',
  slogan: '',
  creationDate: '',
})

const showDialog = ref(false)
const cancelDialog = () => {
  showDialog.value = false
  formModel.value = ''
  formRef.value.clearValidate()
}

const formRef = ref()
const emit = defineEmits(['success'])
const confirmDialog = async () => {
  await formRef.value.validate()
  console.log(formModel.value)
  formModel.value.id
    ? await updateTeamsService(formModel.value)
    : await addTeamsService(formModel.value)

  console.log(formModel.value)

  ElMessage({
    type: 'success',
    message: formModel.value.id ? '编辑成功' : '添加成功',
  })
  showDialog.value = false
  formModel.value = ''
  emit('success')
}
const open = async (row) => {
  showDialog.value = true
  if (!isEmpty(row)) {
    formModel.value = { ...row }
    console.log(formModel.value)
  }
}

defineExpose({
  open,
})
</script>

<template>
  <el-dialog
    v-model="showDialog"
    :title="formModel.id ? '编辑战队' : '添加战队'"
    width="500"
    center
    @close="cancelDialog"
  >
    <el-form
      ref="formRef"
      :model="formModel"
      :rules="formRules"
      label-width="100px"
      style="padding-right: 30px"
    >
      <el-form-item label="战队名称" prop="teamName">
        <el-input v-model="formModel.teamName" minlength="1" maxlength="10"></el-input>
      </el-form-item>
      <el-form-item label="队标" prop="logo">
        <el-input v-model="formModel.logo" minlength="1" maxlength="10"></el-input>
      </el-form-item>
      <el-form-item label="成员" prop="members">
        <el-input v-model="formModel.members" minlength="1" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="口号" prop="slogan">
        <el-input v-model="formModel.slogan" minlength="1" maxlength="15"></el-input>
      </el-form-item>
      <el-form-item label="成立时间" prop="creationDate">
        <el-input
          v-model="formModel.creationDate"
          :disabled="!isEmpty(formModel.id) ? true : false"
          minlength="1"
          maxlength="15"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button @click="cancelDialog">取消</el-button>
        <el-button type="primary" @click="confirmDialog"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
